public class StateMachine {

    public static void main(String[] args) {
        StateMachine machine = new StateMachine();
        machine.start();
    }

    private String input;
    private int index;
    private State state;

    enum State {
        INITIAL, READING_CHAR, ESCAPE_CHAR, ENDED
    }

    public StateMachine() {
        input = "This is a \"quoted string with \\n escape character\" and more text.";
        index = 0;
        state = State.INITIAL;
    }

    public void start() {
        while (state!= State.ENDED) {
            processCharacter();
        }
    }

    private void processCharacter() {
        if (index >= input.length()) {
            state = State.ENDED;
            return;
        }
        char currentChar = input.charAt(index);
        switch (state) {
            case INITIAL:
                if (currentChar == '"') {
                    state = State.READING_CHAR;
                }
                break;
            case READING_CHAR:
                if (currentChar == '\\') {
                    state = State.ESCAPE_CHAR;
                } else if (currentChar == '"') {
                    state = State.ENDED;
                }
                break;
            case ESCAPE_CHAR:
                state = State.READING_CHAR;
                break;
        }
        index++;
    }
}